<?php
if(isset($_REQUEST['frm_aksi'])){
	if($_POST['frm_aksi']=="buat"){
		require_once("../class/class.detail.php");
		require_once("../class/class.pegawai.php");
		require_once("../class/class.refCuti.php");
		$id=$_POST['id_aksi'];

		$detailDao=new DetailDao();
		$details=$detailDao->getDetailById($id);
		$msg=false;
		$cuti=array();
		$error="Gagal Buat Cuti";
		$pegawaiDao= new PegawaiDao();
		$refDao= new RefCutiDao();
		if($details!=false){
			$msg=true;
			$cuti=array(
				'id' => $details['id_det_usulan'],
				'pegawai' =>$pegawaiDao->getPegawai($details['nip']),
				'idJns' => $refDao->getCuti($details['id_cuti']),
				'tglAwal' => $details['tgl_mulai'],
				'tglAkhir' => $details['tgl_selesai'],
				);
		}
		$data=array(
			'msg' => $msg,
			'cuti' => $cuti,
			'buat' => true,
			'simpan' =>false,
			'eror' => $error,
			);
		echo json_encode($data);
	}
	if($_POST['frm_aksi']=="simpan"){
		require_once("../class/class.detail.php");
		require_once("../class/class.cuti.php");
		
		$id=$_POST['id_aksi'];
		$detailDao=new DetailDao();
		$details=$detailDao->getDetailById($id);

		$nip_ganti=$_POST['nip_ganti'];
		$nip_atasan=$_POST['nip_atasan'];
		$nip_setuju=$_POST['nip_setuju'];

		$alamat=$_POST['alamat'];
		$telp=$_POST['telp'];

		$nip="";
		$idJns="";
		$tglAwal="";
		$tglAkhir="";

		$msg=false;
		if($details!=false){
			$msg=true;
			$nip=$details['nip'];
			$idJns=$details['id_cuti'];
			$tglAwal=$details['tgl_mulai'];
			$tglAkhir=$details['tgl_selesai'];
		}

		$tglAju=date('Y-m-d H:i:s');
        $idProses=1;

        $year=explode("-", $tglAwal);
		$tahun=$year[0];
        $tanggal=new Tanggal();
        $liburDao=new LiburDao();
        $libur=$liburDao->getAllByTahun($tahun);
        $libur2=array();
        $i=0;
		if(count($libur)>0){
			foreach ($libur as $row) {
				$libur2[$i]=$row['tgl'];
				$i++;
			}
		}
        
        $jmlHari=$tanggal->getWorkingDays($tglAwal,$tglAkhir,$libur2);


        $cuti=new Cuti();
        $msg=false;
        
        $sukses="Berhasil Buat Cuti";
        $error="Gagal Rekam Cuti";

        $cuti->tglAju=$tglAju;
        $cuti->tglAwal=$tglAwal;
        $cuti->tglAkhir=$tglAhir;
        $cuti->nip=$nip;
        $cuti->nipAtasan=$nip_atasan;
        $cuti->nipGanti=$nip_ganti;
        $cuti->nipSetuju=$nip_setuju;
        $cuti->alamat=$alamat;
        $cuti->telp=$telp;
        $cuti->jmlHari=$jmlHari;
        $cuti->tahun=$tahun;
        $cuti->idProses=$idProses;
        $cuti->idJnsCuti=$idJns;

        $cutiDao=new cutiDao();

        if($cutiDao->cekCuti($nip,$tgl_awal,$tgl_akhir)){
            $error="Tanggal Cuti Sudah Diambil";
        }else{
            $create=$cutiDao->createCuti($cuti);
            if($create){
                $msg=true;
                $update=$detailDao->updateDetailBuatCuti($id);
            }
        }

        $data=array(
            'msg' => $msg,
            'eror' => $error,
            'sukses' => $sukses,
            'buat' => false,
            );

        echo json_encode($data);

	}
}
if(isset($_REQUEST['aksi_get_cuti_ln'])){
	require_once("../class/class.detail.php");
	require_once("../class/class.pegawai.php");
	require_once("../class/class.refCuti.php");

	$detailDao=new DetailDao();
	$pegawaiDao= new PegawaiDao();
	$refDao= new RefCutiDao();

	$details=$detailDao->getDetailSelesai();
	$msg=false;
	$cuta=array();
	
	if($details!=false){
		$msg=true;
		for($i=0;$i<count($details);$i++){
			$cuti[$i]=array(
				'id' => $details[$i]['id_det_usulan'],
				'pegawai' =>$pegawaiDao->getPegawai($details[$i]['nip']),
				'idJns' => $refDao->getCuti($details[$i]['id_cuti']),
				'tglAwal' => $details[$i]['tgl_mulai'],
				'tglAkhir' => $details[$i]['tgl_selesai'],
				);
		}
	}

	$data=array(
		'msg' => $msg,
		'cuti' => $cuti,
		);

	echo json_encode($data);
}

?>